// Created by WXX on 2021/12/29 21:47
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

/**
 * 执行用时：8 ms, 在所有 C++ 提交中击败了97.90%的用户
 * 内存消耗：11.9 MB, 在所有 C++ 提交中击败了17.32%的用户
 */
class Solution {
public:
    int countQuadruplets(vector<int> &nums) {

        int n = nums.size();
        int res = 0;
        unordered_map<int, int> cnt;
        for (int b = n - 3; b; b--) {
            for (int d = b + 2; d < n; d++) cnt[nums[d] - nums[b + 1]]++;
            for (int a = 0; a < b; a++) {
                int s = nums[a] + nums[b];
                if (cnt.count(s)) res += cnt[s];
            }
        }
        return res;
    }
};

int main() {

    vector<int> nums = {53, 10, 34, 80, 84, 70, 30, 4, 94, 79, 80, 60, 87};
    cout << Solution().countQuadruplets(nums) << endl;

    return 0;
}
